<template>
	<!-- #ifdef MP-WEIXIN  -->
	<view class="force-login-wrap">
		<!-- <image class="logo-bg" src="../static/tRefresh/fail.png" mode="aspectFill"></image> -->
		<view class="force-login__content y-f">
			<open-data class="user-avatar" type="userAvatarUrl"></open-data>
			<open-data class="user-name" type="userNickName"></open-data>
			<view class="login-notice"><text>为了提供更优质的服务，需要获取您的头像昵称</text></view>
			<button class="cu-btn author-btn" @getuserinfo="getuserinfo" open-type="getUserInfo">
				<text>授权并查看</text>
			</button>
			<button class="cu-btn close-btn" @tap="closeAuth"><text>暂不授权</text></button>
		</view>
		<u-toast ref="uToast" />
	</view>
	<!-- #endif  -->
</template>

<script>
/**
 * 登录提示页
 * @property {Boolean} value=showLoginTip - 由v-model控制显示隐藏。
 * @property {Boolean} forceOauth - 小程序端特制的全屏登录提示。
 */

export default {
	data() {
		return {};
	},
	methods: {
		async closeAuth() {
			this.$u.vuex('vuex_login', 0);
		},
		async getuserinfo(e) {
			console.log(e);
			const [, res] = await uni.login();
			let obj = {
				code: res.code
			};
			const ress = await this.$u.post('api/user/getWxMiniProgramSessionKey', obj, {});
			//ress.data.session_key; //openid

			console.log('ress :>> ', ress);
			this.name(ress.data.session_key, ress.data.openid, e.detail.iv, e.detail.encryptedData);
		},
		async name(session_key, openid, iv, encryptedData) {
			console.log('222 :>> ', 222);
			let obj = {
				openid,
				iv,
				encryptedData,
				session_key
			};
			const res = await this.$u.post('api/user/wxMiniProgramLogin', obj, {});
			console.log('res :>> ', res);
			if (res.code == 1) {
				uni.setStorageSync('token', res.data.token);
				this.$refs.uToast.show({ title: '登录成功', type: 'success' });
				setTimeout(() => {
					this.$u.vuex('vuex_login', 0)
					uni.reLaunch({
						url: '../../pages/mbrCentre/mbrCentre',
					});
				}, 1000);
			}
		}
	}
};
</script>

<style lang="scss">
// 登录提示
.modal-box {
	width: 610rpx;
	border-radius: 20rpx;
	background: #fff;
	position: relative;
	left: 50%;
	transform: translateX(-50%);
	padding-bottom: 30rpx;

	.head-bg {
		width: 100%;
		height: 210rpx;
	}

	.detail {
		.title1 {
			color: #46351b;
			font-size: 35rpx;
			font-weight: bold;
		}

		.title2 {
			font-size: 28rpx;
			color: #999;
			padding-top: 20rpx;
		}
	}

	.btn-box {
		margin-top: 80rpx;

		.login-btn {
			width: 492rpx;
			height: 70rpx;
			background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
			box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
			border-radius: 35rpx;
			font-size: 28rpx;
			color: rgba(#fff, 0.9);
		}

		.close-btn {
			width: 492rpx;
			height: 70rpx;
			color: #e9b766;
			font-size: 26rpx;
			margin-top: 20rpx;
			background: none;
		}
	}
}

// 小程序登录提醒
/* #ifdef MP-WEIXIN */
.force-login-wrap {
	position: fixed;
	width: 100vw;
	height: 100vh;
	overflow: hidden;
	z-index: 11111;
	top: 0;
	background: linear-gradient(180deg, #f7f7f7 0%, #f7f7f7 98%);
	.logo-bg {
		width: 640rpx;
		height: 300rpx;
	}
	.force-login__content {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		.user-avatar {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-bottom: 40rpx;
		}
		.user-name {
			font-size: 35rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #000;
			margin-bottom: 30rpx;
		}
		.login-notice {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #000;
			line-height: 44rpx;
			width: 400rpx;
			text-align: center;
			margin-bottom: 80rpx;
		}
		.author-btn {
			width: 630rpx;
			height: 80rpx;
			background: linear-gradient(180deg, #ffffff, #ffffff);
			box-shadow: 0px 7rpx 6rpx 0px #ffffff;
			border-radius: 40rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000;
		}
		.close-btn {
			width: 630rpx;
			height: 80rpx;
			margin-top: 30rpx;
			border-radius: 40rpx;
			border: 2rpx solid #ffffff;
			background: none;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #000;
		}
	}
}
/* #endif */
</style>